package com.example.NIO;

import java.nio.ByteBuffer;

public class ReadOnlyBuffer {
    public static void main(String[] args) {
        // 创建一个 buffer
        ByteBuffer buf = ByteBuffer.allocate(64);
        for (int i = 0; i < 64; i++) {
            buf.put((byte) i);
        }

        // 读取
        buf.flip();
        // 得到一个只读的 Buffer
        ByteBuffer onlyBuffer = buf.asReadOnlyBuffer();
        System.out.println(onlyBuffer.getClass());

        while (onlyBuffer.hasRemaining()) {
            System.out.println(onlyBuffer.get());
        }
        // throw ReadOnlyBufferException
        onlyBuffer.put(Byte.parseByte("11"));
    }
}
